package com.yanqu.road.server.gameplayer.module.union;

import com.yanqu.road.entity.log.cross.LogUnionContribution;
import com.yanqu.road.logic.bussiness.union.UserUnionBussiness;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.pb.UnionPb;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.entity.union.UserUnionChamberData;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.UnionJoinApplyMgr;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class UnionModule extends GeneralModule {

    private UserUnionChamberData userUnionChamberData;

    public UnionModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userUnionChamberData = UserUnionBussiness.getUserUnionChamberData(player.getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        if(SystemOpenMgr.systemOpen(player, eSystemId.Union.getValue())){
            initUserChamberData();
        }
        return true;
    }

    @Override
    public boolean saveData() {
        if(null != userUnionChamberData){
            if(userUnionChamberData.isInsertOption()){
                UserUnionBussiness.addUserUnionChamberData(userUnionChamberData);
            }else if(userUnionChamberData.isUpdateOption()){
                UserUnionBussiness.updateUserUnionChamberData(userUnionChamberData);
            }
        }
        return true;
    }

    @Override
    public void loginSendMsg() {
        String unionUid = player.getUserInfo().getUnionUid();
        UnionMember member = UnionMgr.getUnionMember(unionUid, player.getUserId());
        if (!StringUtils.isNullOrEmpty(unionUid)) {
            if (null != member) {
                if (member.getPosition() == eUnionPosition.Master.getValue() || member.getPosition() == eUnionPosition.DeputyMaster.getValue()) {
                    UnionJoinApplyMgr.sendJoinApplyList(player, unionUid);
                }
                player.sendPacket(Protocol.U_UNION_DETAIL, UnionMgr.getUnionDetailMsg(unionUid, eUnionSyncType.Normal.getValue()));
            }
            UnionInfo unionInfo = UnionMgr.getUnionInfo(unionUid);
            if(null != unionInfo){
                if(null != member){
                    if(member.getChamberPatronsId() > 0){
                        UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(member.getChamberPatronsId());
                        if(null != userPatrons){
                            member.setChamberPatron(userPatrons);
                        }
                    }
                }
                syncChamberPatronsList();
            }
        } else {
            if (member != null) {
                syncUnionMember(member);
            }
        }
    }

    @Override
    public void afterLogin() {
        syncChamberData();
    }

    public void resetOneDay(){
        if(null != userUnionChamberData){
            long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000;
            if(zeroTime > userUnionChamberData.getLastResetTime()){
                userUnionChamberData.setUseHuntUserIdList(new ArrayList<>());
                userUnionChamberData.setLastResetTime(zeroTime);
                syncChamberData();
            }
        }
    }

    public void initSystem(){
        initUserChamberData();
        syncChamberData();
    }

    private void initUserChamberData(){
        if(null == userUnionChamberData){
            UserUnionChamberData chamberData = new UserUnionChamberData();
            chamberData.setUserId(player.getUserId());
            chamberData.setLastResetTime(System.currentTimeMillis() / 1000);
            chamberData.setUseHuntUserIdList(new ArrayList<>());
            chamberData.setInsertOption();
            userUnionChamberData = chamberData;
        }
    }

    public void syncChamberData(){
        UnionProto.UnionChamberDataMsg.Builder msg = UnionPb.parseUnionChamberDataMsg(userUnionChamberData);
        if(null != msg){
            player.sendPacket(Protocol.U_UNION_CHAMBER_DATA_SYNC, msg);
        }
    }

    public void syncChamberPatronsList(){
        String unionUid = player.getUserInfo().getUnionUid();
        List<UserPatrons> patronsList = new ArrayList<>();
        if(!StringUtils.isNullOrEmpty(unionUid)) {
            Map<Long, UnionMember> memberMap = UnionMgr.getUnionMemberMap(unionUid);
            if(null != memberMap) {
                for(UnionMember unionMember : memberMap.values()){
                    if(null != unionMember.getChamberPatron()){
                        patronsList.add(unionMember.getChamberPatron());
                    }
                }
            }
        }
        player.sendPacket(Protocol.U_UNION_CHAMBER_PATRONS_LIST, UnionPb.parseUnionChamberPatronsListMsg(patronsList));
    }

    public void syncUnionMember(UnionMember unionMember){
        UnionProto.UnionMemberTempMsg.Builder msg = UnionPb.parseUnionMemberTempMsg(unionMember);
        if(null != msg){
            player.sendPacket(Protocol.U_UNION_MEMBER_SYNC, msg);
        }
    }

    public void addApplyUnionUid(String unionUid){
        UnionMember member = UnionMgr.getUnionMember("", player.getUserId());
        if(null == member){
            member = new UnionMember();
            member.setUnionUid("");
            member.setUserId(player.getUserId());
            member.setUserBaseInfo(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId()));
            member.setInsertOption();
            UnionMgr.addMemberToDeleteMap(member);
        }
        member.addApplyUnionUid(unionUid);
        syncUnionMember(member);
    }

    public void addUnionExp(long exp, eLogMoneyType master, eLogMoneyType son) {
        String unionUid = player.getUserInfo().getUnionUid();
        if (!StringUtils.isNullOrEmpty(unionUid)) {
            UnionMgr.addUnionExp(player, unionUid, exp, master, son);
        }
    }

    public void addUnionWealth(long wealth, eLogMoneyType master, eLogMoneyType son) {
        String unionUid = player.getUserInfo().getUnionUid();
        if (!StringUtils.isNullOrEmpty(unionUid)) {
            UnionMgr.addUnionWealth(player, unionUid, wealth, master, son);
        }
    }

    public boolean isUnionWealthEnough(long value){
        long nowValue = 0;
        String unionUid = player.getUserInfo().getUnionUid();
        UnionInfo unionInfo = UnionMgr.getUnionInfo(unionUid);
        if (unionInfo != null) {
            nowValue = unionInfo.getWealth();
        }
        return nowValue >= value;
    }

    public long removeUnionWealth(long wealth, eLogMoneyType master, eLogMoneyType son) {
        String unionUid = player.getUserInfo().getUnionUid();
        if (!StringUtils.isNullOrEmpty(unionUid)) {
            return UnionMgr.removeUnionWealth(player, unionUid, wealth, master, son);
        }
        return 0;
    }

    public long addContribution(long value, eLogMoneyType master, eLogMoneyType son) {
        if (value > 0) {
            String unionUid = player.getUserInfo().getUnionUid();
            UnionMember unionMember = UnionMgr.getUnionMember(unionUid, player.getUserId());
            if (null != unionMember) {
                long oldContribution = unionMember.getContribution();
                unionMember.setContribution(unionMember.getContribution() + value);
                unionMember.setTotalContribution(unionMember.getTotalContribution() + value);
                unionMember.setTodayContribution((int) (unionMember.getTodayContribution() + value));
                syncUnionMember(unionMember);
                AutoLogMgr.add(new LogUnionContribution(player.getUserId(), unionUid, true, value, oldContribution, unionMember.getContribution(), son.getValue()));
            }
            return value;
        }
        return 0;
    }

    public long removeContribution(long value, eLogMoneyType master, eLogMoneyType son) {
        if (value > 0) {
            String unionUid = player.getUserInfo().getUnionUid();
            UnionMember unionMember = UnionMgr.getUnionMember(unionUid, player.getUserId());
            if (unionMember != null && unionMember.getContribution() >= value) {
                long oldContribution = unionMember.getContribution();
                unionMember.setContribution(unionMember.getContribution() - value);
                syncUnionMember(unionMember);
                AutoLogMgr.add(new LogUnionContribution(player.getUserId(), unionUid, false, value, oldContribution, unionMember.getContribution(), son.getValue()));
                return value;
            }
        }
        return 0;
    }

    public boolean isContributionEnough(long value){
        long nowValue = 0;
        String unionUid = player.getUserInfo().getUnionUid();
        UnionMember unionMember = UnionMgr.getUnionMember(unionUid, player.getUserId());
        if (unionMember != null) {
            nowValue = unionMember.getContribution();
        }
        return nowValue >= value;
    }

    public UserUnionChamberData getUserUnionChamberData() {
        return userUnionChamberData;
    }
}
